<script lang="ts">
import { defineComponent, ref, reactive, onMounted } from 'vue';
import { findPages, change, deleteOne, update, fuzzy, insert } from '../services/Client.ts';

interface Client {
  clientid?: string;
  name: string;
  telephone: string;
  address: string;
  status?: string;
  createtime?: string;
}

interface FuzzySearch {
  name?: string;
  telephone?: string;
  address?: string;
  status?: string;
  createtime?: string;
  pageNum?: number;
  pageSize?: number;
}

export default defineComponent({
  name: 'ClientManage',
  setup() {
    // 零销商数据
    const clients = ref<Client[]>([]);
    // 当前的页码
    const pageNum = ref(1);
    // 每页显示几条
    const pageSize = ref(7);
    // 数据总条数
    const total = ref(1);
    // 模糊搜索条件
    const fuzzySearch = reactive<FuzzySearch>({});
    // 记录当前是否是模糊搜索
    const isFuzzy = ref(false);
    // 保存编辑
    const editClient = reactive<Client>({
      clientid: '',
      name: '',
      telephone: '',
      address: '',
      status: '',
      createtime: '',
    });
    // 编辑框是否可见
    const editDialogVisible = ref(false);
    // 新增框是否可见
    const addDialogVisible = ref(false);
    // 保存新增
    const addClient = reactive<Omit<Client, 'clientid'>>({
      name: '',
      telephone: '',
      address: '',
    });

    // 页面挂载时调用
    onMounted(() => {
      fetchClients();
    });

    // 获取零销商数据
    const fetchClients = () => {
      findPages(pageNum.value, pageSize.value).then((response: any) => {
        clients.value = response.data.data.rows;
        total.value = response.data.data.total;
      });
    };

    // 页码改变时调用
    const handleCurrentChange = (newPageNum: number) => {
      pageNum.value = newPageNum;
      refresh();
    };

    // 改变启用状态调用
    const changeStatus = (clientid: string) => {
      change(clientid).then(() => {
        alert('改变启用状态成功');
      });
    };

    // 点击删除按钮调用
    const handleDelete = (clientid: string) => {
      deleteOne(clientid).then(() => {
        alert('删除成功');
        refresh();
      });
    };

    // 点击搜索按钮调用
    const handleSearch = () => {
      fuzzySearch.pageSize = pageSize.value;
      fuzzySearch.pageNum = pageNum.value;
      fuzzy(fuzzySearch).then((response: any) => {
        clients.value = response.data.data.rows;
        total.value = response.data.data.total;
        isFuzzy.value = true;
      });
    };

    // 重新搜索
    const refresh = () => {
      if (isFuzzy.value) {
        fuzzySearch.pageSize = pageSize.value;
        fuzzySearch.pageNum = pageNum.value;
        fuzzy(fuzzySearch).then((response: any) => {
          clients.value = response.data.data.rows;
          total.value = response.data.data.total;
        });
      } else {
        fetchClients();
      }
    };

    // 点击编辑按钮调用
    const handleEdit = (client: Client) => {
      console.log(client);
      Object.assign(editClient, client);
      editClient.status = editClient.status?.toString() || '';
      editDialogVisible.value = true;
    };

    // 取消编辑
    const cancel = () => {
      editDialogVisible.value = false;
      addDialogVisible.value = false;
    };

    // 确定编辑
    const commitEdit = () => {
      editDialogVisible.value = false;
      update(editClient).then(() => {
        alert('编辑成功');
        refresh();
      });
    };

    // 点击新增按钮调用
    const handleAdd = () => {
      addDialogVisible.value = true;
    };

    // 确认新增
    const commitAdd = () => {
      addDialogVisible.value = false;
      insert(addClient).then(() => {
        alert('新增成功');
        refresh();
        // 新增对象置空
        Object.keys(addClient).forEach((key) => {
          (addClient as any)[key] = '';
        });
      });
    };

    return {
      clients,
      pageNum,
      pageSize,
      total,
      fuzzySearch,
      isFuzzy,
      editClient,
      editDialogVisible,
      addDialogVisible,
      addClient,
      handleCurrentChange,
      changeStatus,
      handleDelete,
      handleSearch,
      refresh,
      handleEdit,
      cancel,
      commitEdit,
      handleAdd,
      commitAdd,
    };
  },
});
</script>

<template>
  <div>
    <!--顶部搜索栏-->
    姓名:
    <el-input v-model="fuzzySearch.name" style="width: 140px" size="small"></el-input>
    手机:
    <el-input
        v-model="fuzzySearch.telephone"
        style="width: 140px"
        size="small"
    ></el-input>
    地址:
    <el-input
        v-model="fuzzySearch.address"
        style="width: 140px"
        size="small"
    ></el-input>
    状态:
    <el-select v-model="fuzzySearch.status" placeholder="全部" size="small" clearable>
      <el-option label="启用" value="1"></el-option>
      <el-option label="停用" value="0"></el-option>
    </el-select>
    创建日期:
    <el-date-picker
        v-model="fuzzySearch.createtime"
        type="date"
        placeholder="选择创建日期"
        value-format="yyyy-MM-dd"
    >
    </el-date-picker>
    <br />
    <el-button
        type="primary"
        size="default"
        @click="handleSearch"
        style="margin-left: 10px"
    >搜索</el-button
    >
    <el-button
        type="primary"
        size="default"
        @click="handleAdd"
        style="margin-left: 10px"
    >新增</el-button
    ><br />
    <!--表格-->
    <el-table :data="clients" style="width: 1200px" stripe>
      <el-table-column label="序号" width="60" align="center">
        <template v-slot="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="120" align="center">
      </el-table-column>
      <el-table-column prop="telephone" label="手机" width="120" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" width="120" align="center">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="120" align="center">
        <template v-slot="scope"
        ><el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
            @change="changeStatus(scope.row.clientid)"
        >
        </el-switch>
        </template>
      </el-table-column>
      <el-table-column
          prop="createtime"
          label="创建日期"
          width="240"
          align="center"
      >
      </el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template v-slot="scope">
          <el-button
              type="primary"
              size="default"
              style="margin-right: 5px"
              @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-popconfirm
              title="确定要删除吗？"
              @onConfirm="handleDelete(scope.row.clientid)"
          >
            <template v-slot:reference>
              <el-button
                  type="danger"
                  size="default"
                  style="margin-right: 5px"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
    >
    </el-pagination>
    <!--编辑对话框-->
    <el-dialog title="编辑" v-model="editDialogVisible" @close="cancel">
      <el-form :model="editClient" label-width="70px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editClient.name"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="telephone">
          <el-input v-model="editClient.telephone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="editClient.address"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
              v-model="editClient.status"
              placeholder="请选择"
              size="small"
          >
            <el-option label="启用" value="1"></el-option>
            <el-option label="停用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建日期" prop="createtime">
          <el-date-picker
              v-model="editClient.createtime"
              type="date"
              placeholder="选择创建日期"
              value-format="yyyy-MM-dd"
              readonly
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="commitEdit">确 定</el-button>
      </div>
    </el-dialog>
    <!--新增对话框-->
    <el-dialog title="新增" v-model="addDialogVisible" @close="cancel">
      <el-form :model="addClient" label-width="70px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addClient.name"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="telephone">
          <el-input v-model="addClient.telephone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="addClient.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="commitAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>
